

package top.continew.admin.question.user.service.question.entity;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionSelect;
import top.continew.admin.question.user.service.question.enums.ProblemStateEnum;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;

import java.util.List;

public class TrueOrFalseQuestion extends AbstractQuestion implements ISelectQuestion {

    private List<QuestionSelect> select;

    @Override
    public void init(Question question) {
        super.init(question);
        select = new QuestionSelect().selectList(Wrappers.lambdaQuery(QuestionSelect.class)
            .eq(QuestionSelect::getQuestionId, question.getId()));
    }

    @Override
    public CorrectResult correct(String userAnswer) {
        CorrectResult correctResult = new CorrectResult();
        correctResult.setCorrectAnswer(answer);
        correctResult.setUserAnswer(userAnswer);
        correctResult.setProblemState(ProblemStateEnum.UNCHECK.getCode());
        // 处理空答案情况
        if (userAnswer == null || userAnswer.trim().isEmpty()) {
            return correctResult;
        }
        String[] userAnswerList = userAnswer.split(",");
        List<String> answerList = JSON.parseArray(answer, String.class);
        // 检查答案格式
        if (userAnswerList.length != 1 || answerList.isEmpty()) {
            correctResult.setProblemState(ProblemStateEnum.WRONG.getCode());
            return correctResult;
        }
        // 比对答案
        if (userAnswerList[0].trim().equals(answerList.get(0).trim())) {
            correctResult.setProblemState(ProblemStateEnum.RIGHT.getCode());
        } else {
            correctResult.setProblemState(ProblemStateEnum.WRONG.getCode());
        }
        return correctResult;
    }

    @Override
    public Integer getType() {
        return QuestionTypeEnum.TRUE_OR_FALSE_QUESTIONS.getCode();
    }

    @Override
    public String getTypeName() {
        return QuestionTypeEnum.TRUE_OR_FALSE_QUESTIONS.getName();
    }

    @Override
    public List<QuestionSelect> getSelect() {
        return this.select;
    }
}